package com.cindata

import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class FinancialPledgeImportController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
    def springSecurityService

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond FinancialPledgeImport.list(params), model: [financialPledgeImportCount: FinancialPledgeImport.count()]
    }

    def show(FinancialPledgeImport financialPledgeImport)
    {
        respond financialPledgeImport
    }

    def create()
    {
        respond new FinancialPledgeImport(params)
    }

    @Transactional
    def searchImport()
    {
        def order = params["order"]
        def sort = params["sort"]
        def queryParam = new HashMap()
        //押品地址

        String sql = "from FinancialPledgeImport e where 1=1  "
        def user = User.findById(springSecurityService.principal.id)
        def accountSql = Account.FilterAccountSql(user)
        if (params["id"])
        {
            def id = Long.parseLong(params["id"])
            sql += "and e.batch.id = :id "
            queryParam["id"] = id
        }
        if (params["registerFullName"])
        {
            sql += " and e.registerFullName = '${params["registerFullName"]}'"
        }
        if (params["registerAccount"])
        {
            sql += " and e.registerAccount ='${params["registerAccount"]}'"
        }
        //执行查询方法
        def count = FinancialPledgeImport.executeQuery("select count(e.id) " + sql, queryParam)
        def financialPledgeList
        if (count[0] > 0)
        {
            params.max = params.max ?: 10
            params.offset = params.offset ?: 0
            if (order && sort)
            {
                sql += " order by e.${sort} ${order}"
            }
            else
            {
                sql += " order by e.modifiedDate desc "
            }
            financialPledgeList = FinancialPledgeImport.executeQuery(sql, queryParam, [max: params.max ?: 10, offset: params.offset ?: 0])
        }
        if (financialPledgeList != null)
        {

            respond FinancialPledgeImport, model: [financialPledgeList: financialPledgeList, count: count[0], params: params], view: 'index'
        }
        else
        {
            respond FinancialPledgeImport, view: 'index'
        }
    }

    @Transactional
    def save(FinancialPledgeImport financialPledgeImport)
    {
        if (financialPledgeImport == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (financialPledgeImport.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond financialPledgeImport.errors, view: 'create'
            return
        }

        financialPledgeImport.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'financialPledgeImport.label', default: 'FinancialPledgeImport'), financialPledgeImport.id])
                redirect financialPledgeImport
            }
            '*' { respond financialPledgeImport, [status: CREATED] }
        }
    }

    def edit(FinancialPledgeImport financialPledgeImport)
    {
        respond financialPledgeImport
    }

    @Transactional
    def update(FinancialPledgeImport financialPledgeImport)
    {
        if (financialPledgeImport == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (financialPledgeImport.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond financialPledgeImport.errors, view: 'edit'
            return
        }

        financialPledgeImport.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'financialPledgeImport.label', default: 'FinancialPledgeImport'), financialPledgeImport.id])
                redirect financialPledgeImport
            }
            '*' { respond financialPledgeImport, [status: OK] }
        }
    }

    @Transactional
    def delete(FinancialPledgeImport financialPledgeImport)
    {

        if (financialPledgeImport == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        financialPledgeImport.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'financialPledgeImport.label', default: 'FinancialPledgeImport'), financialPledgeImport.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'financialPledgeImport.label', default: 'FinancialPledgeImport'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
